package com.itheimamobileguard.db.dao;

import java.util.ArrayList;
import java.util.List;

import android.R.integer;
import android.content.ContentValues;
import android.content.Context;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;
import android.os.SystemClock;

import com.itheimamobileguard.db.BlackNumberDBOpenHelper;
import com.itheimamobileguard.domain.BlockNumberInfo;

/**
 * 黑名单数据库的增删改查
 * @author dream
 *
 */
public class BlackNumberDao {
	
	private BlackNumberDBOpenHelper helper;
	
	public BlackNumberDao(Context context){
		helper = new BlackNumberDBOpenHelper(context);
	}
	
	/**
	 * 添加黑名单号码
	 * @param number 号码
	 * @param mode 拦截模式
	 * @return 是否添加成功
	 */
	public boolean add(String number,String mode){
		SQLiteDatabase db =helper.getWritableDatabase();
		ContentValues values = new ContentValues();
		values.put("number", number);
		values.put("mode", mode);
		long rawid = db.insert("blackinfo", null, values);
		if (rawid==-1) {
			return false;
		}else {
			return true;
		}
	}
	
	/**
	 * 删除黑名单号码
	 * @param number 号码
	 * @param mode 拦截模式
	 * @return 是否添加成功
	 */
	public boolean delete(String number){
		SQLiteDatabase db =helper.getWritableDatabase();
		int rownumber = db.delete("blackinfo", "number=?", new String[]{number});
		if (rownumber == 0) {
			return false;
		}else {
			return true;
		}
	}
	
	/**
	 * 修改黑名单号码拦截模式
	 * @param number 号码
	 * @param newmode 新的拦截模式
	 * @return 是否修改成功
	 */
	public boolean changeBlockMode(String number,String newmode){
		SQLiteDatabase db =helper.getWritableDatabase();
		ContentValues values = new ContentValues();
		values.put("mode", newmode);
		int rownumber = db.update("blackinfo", values, "number = ?",new String[]{number});
		if (rownumber == 0) {
			return false;
		}else {
			return true;
		}
	}
	
	/**
	 * 返回一个黑名单拦截模式
	 * @return
	 */
	public String findBlockMOde(String number){
		String mode = "0";
		SQLiteDatabase db =helper.getReadableDatabase();
		Cursor cursor = db.query("blackinfo", new String[]{"mode"}, "number=?", new String[]{number}, null, null, null);
		if (cursor.moveToNext()) {
			mode= cursor.getString(0);
		}
		cursor.close();
		db.close();
		return mode;
	}
	
	/**
	 * 返回全部黑名单拦截模式
	 * @return
	 */
	public List<BlockNumberInfo> findAll(){
		SQLiteDatabase db =helper.getReadableDatabase();
		Cursor cursor = db.query("blackinfo", new String[]{"number","mode"}, null, null, null, null, null);
		List<BlockNumberInfo> blockNumberInfos = new ArrayList<BlockNumberInfo>();
		while(cursor.moveToNext()){
			BlockNumberInfo info = new BlockNumberInfo();
			String number = cursor.getString(0);
			String mode = cursor.getString(1);
			info.setNumber(number);
			info.setMode(mode);
			blockNumberInfos.add(info);
		}
		cursor.close();
		db.close();
		SystemClock.sleep(3000);
		return blockNumberInfos;
	}
	
	/**
	 * 分页查询
	 * @return
	 */
	public List<BlockNumberInfo> findPart(int pagenumber,int pagesize){
		SQLiteDatabase db =helper.getReadableDatabase();
		Cursor cursor = db.rawQuery("select number,mode from blackinfo limit ? offset ?", new String[]{String.valueOf(pagesize),String.valueOf(pagesize*(pagenumber-1))});
		List<BlockNumberInfo> blockNumberInfos = new ArrayList<BlockNumberInfo>();
		while(cursor.moveToNext()){
			BlockNumberInfo info = new BlockNumberInfo();
			String number = cursor.getString(0);
			String mode = cursor.getString(1);
			info.setNumber(number);
			info.setMode(mode);
			blockNumberInfos.add(info);
		}
		cursor.close();
		db.close();
		SystemClock.sleep(30);
		return blockNumberInfos;
	}
	
	/**
	 * 分批查询
	 * @param startIndex 从哪个位置开始加载数据
	 * @param maxCount 最多加载几条数据
	 * @return
	 */
	public List<BlockNumberInfo> findPart2(int startIndex,int maxCount){
		SQLiteDatabase db =helper.getReadableDatabase();
		Cursor cursor = db.rawQuery("select number,mode from blackinfo limit ? offset ?", new String[]{String.valueOf(maxCount),String.valueOf(startIndex)});
		List<BlockNumberInfo> blockNumberInfos = new ArrayList<BlockNumberInfo>();
		while(cursor.moveToNext()){
			BlockNumberInfo info = new BlockNumberInfo();
			String number = cursor.getString(0);
			String mode = cursor.getString(1);
			info.setNumber(number);
			info.setMode(mode);
			blockNumberInfos.add(info);
		}
		cursor.close();
		db.close();
		SystemClock.sleep(30);
		return blockNumberInfos;
	}
	
	/**
	 * 获取数据库总条数
	 * @return
	 */
	public int getTotalNumber(){
		SQLiteDatabase db =helper.getReadableDatabase();
		Cursor cursor = db.rawQuery("select count(*) from blackinfo",null);
		cursor.moveToNext();
		int count = cursor.getInt(0);
		cursor.close();
		db.close();
		return count;
	}
}
